//
//  ZJBaseTabBarController.m
//  Project
//
//  Created by 郑键 on 17/1/11.
//  Copyright © 2017年 zhengjian. All rights reserved.
//

#import "ZJBaseTabBarController.h"
#import "ZJBaseNavigationController.h"
#import "ZJLocalDataManager.h"

@interface ZJBaseTabBarController ()

@end

@implementation ZJBaseTabBarController

#pragma mark - First.通知

#pragma mark - Second.赋值

#pragma mark - Third.点击事件

#pragma mark - Fourth.代理方法

#pragma mark - Fifth.控制器生命周期

/**
 初始化TabBar控制器
 
 @return TabBar控制器
 */
- (instancetype)init
{
    if (self = [super init]) {
        
        /**
         *  配置样式
         */
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:ZJCOLOR.color_c8,
                                                            NSFontAttributeName:SYSTEM_REGULARFONT(10.f)}
                                                 forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:ZJCOLOR.color_c8,
                                                            NSFontAttributeName:SYSTEM_REGULARFONT(10.f)}
                                                 forState:UIControlStateSelected];
    }
    return self;
}

/**
 视图加载完毕
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addSubControllers];
}

#pragma mark - Sixth.界面配置

/**
 添加所有子控制器
 */
- (void)addSubControllers
{
    NSString *rootViewControllerChildControllersFilePath            = [[NSBundle mainBundle] pathForResource:@"BaseTabBarViewControllerChildViewController.plist"
                                                                                                      ofType:nil];
    NSData *data                                                    = [NSData dataWithContentsOfFile:rootViewControllerChildControllersFilePath];
    NSDictionary *dict                                              = [NSDictionary dictionaryWithPlistData:data];
    NSArray *childControllersArray                                  = dict[@"ChildViewControllersArray"];
    [childControllersArray enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
        Class ViewController                                        = NSClassFromString(obj[@"viewController"]);
        UIViewController *viewController                            = [[ViewController alloc] init];
        [self addSubControllerWithTitleKey:obj[@"title"]
                             imageFilePath:obj[@"imageFilePath"]
                                imageNamed:obj[@"imageNamed"]
                            viewController:viewController
                                     index:idx];
    }];
}

/**
 添加子控制器c
 
 @param titleKey                titleStringKey
 @param imageFilePath           图片路径
 @param imageNamed              图片名称
 @param viewController          子控制器
 */
- (void)addSubControllerWithTitleKey:(NSString *)titleKey
                       imageFilePath:(NSString *)imageFilePath
                          imageNamed:(NSString *)imageNamed
                      viewController:(UIViewController *)viewController
                               index:(NSUInteger)index
{
    ZJBaseNavigationController *navigationController            = [[ZJBaseNavigationController alloc]initWithRootViewController:viewController];
    navigationController.tabBarItem.title                       = ZJSTRING(titleKey);
    UIImage *imageNor                                           = ZJIMAGE(imageFilePath,
                                                                          imageNamed,
                                                                          ZJProjectLoadImageDefault).firstObject;
    imageNor                                                    = [imageNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navigationController.tabBarItem.image                       = imageNor;
    UIImage *imageHig                                           = ZJIMAGE(imageFilePath,
                                                                          imageNamed,
                                                                          ZJProjectLoadImageDefault).lastObject;
    imageHig                                                    = [imageHig imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navigationController.tabBarItem.selectedImage               = imageHig;
    
    /**
     *  当为发布按钮时，做特殊处理
     *  后期维护人员需根据不同需求做不同处理
     */
    if (index == 2) {
        [navigationController.tabBarItem setImageInsets:UIEdgeInsetsMake(ADJUST_PERCENT_FLOAT(4.f),
                                                                         0,
                                                                         ADJUST_PERCENT_FLOAT(-4.f),
                                                                         0)];
    }
    
    [self addChildViewController:navigationController];
}

#pragma mark - Seventh.懒加载

@end
